package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.SetmealService;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加商品到购物车
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //添加用户id
        System.out.println(BaseContext.getCurrentId());
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断添加购物车中的商品是否已经存在
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //如果存在,只用商品数据量加一
        if (list!=null&&list.size()>0){
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.updateNumberById(cart);
        }
        //如果不存在,插入一条购物车数据
        else {
            //如果是菜品,插入菜品对应的冗余数据,这样下次查询的时候,直接就是单表查询,节约时间
            if (shoppingCart.getDishId() != null){
                Dish dish = dishMapper.getById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {
                //插入的数据是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    /**
     * 查看购物车
     * @return
     */
    public List<ShoppingCart> showShoppingCart() {
        //根据用户id查看相应的购物车数据
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.list(ShoppingCart
                .builder()
                .userId(userId)
                .build());
    }

    /**
     * 清空购物车
     */
    public void cleanShoppingCart() {
        //根据用户id清空购物车
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.cleanShoppingCart(userId);
    }

    /**
     * 删除购物车中的一个商品
     * @param shoppingCartDTO
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        //设置查询条件,查出要删除菜品的id
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //精确查询,结果肯定只有一条或者没有
        if (list != null&&list.size()>0){
            shoppingCart = list.get(0);
        }
        //进行判断number是否只剩一条
        if (shoppingCart.getNumber()==1){
            //进行删除操作
            shoppingCartMapper.deleteShoppingCart(shoppingCart.getId());
        }else {
            //有多条,-1
            shoppingCart.setNumber(shoppingCart.getNumber()-1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }
    }
}
